package client.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JLabel;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import client.logic.Facade;
import client.logic.GuiFacade;
import encription.SecurityHandler;


/**
 * a panel for user registration , where the user can enter his details , and his picture
 * and submit them to the server for registration
 */
public class pnlRegister extends JPanel{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private JButton btnConfirm;
	private JButton btnCancel;
	private JLabel lblCountry;
	private JLabel lblCity;
	private JTextField txtName;
	private JButton btnBrowse;
	private JTextField txtPicture;
	private JLabel lblPicture;
	private JLabel lblError;
	private JLabel lblHeadline;
	private JTextField txtCity;
	private JTextField txtCountry;
	private JTextField txtBday;
	private JPasswordField txtPassword2;
	private JPasswordField txtPassword;
	private JLabel lblBday;
	private JLabel lblPass2;
	private JLabel lblPassword;
	private JLabel lblName;

	/**
	 * Creates an instance of pnlRegister
	 */
	public pnlRegister(){
		initGUI();
	}

	/**
	 * initializes the GUI
	 */
	private void initGUI() {
		try {
			{
				this.setPreferredSize(new java.awt.Dimension(397, 319));
				this.setLayout(null);
				{
					btnConfirm = new JButton();
					this.add(btnConfirm);
					btnConfirm.setText("Register");
					btnConfirm.setBounds(50, 282, 95, 20);
					btnConfirm.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							if (checkPasswords(txtPassword.getPassword(), txtPassword2.getPassword())){
								if (txtName.getText().length()!=0 && txtPassword.getPassword().length!=0
										&& txtBday.getText().length()!=0 && txtCity.getText().length()!=0
										&& txtCountry.getText().length()!=0 && txtPicture.getText().length()!=0){
									Facade.getInstance().register(txtName.getText(), txtPassword.getPassword(),
											txtBday.getText(), txtCity.getText(), txtCountry.getText(), txtPicture.getText());
								} else {
									lblError.setText("Mandatory field missing");
								}
							}else {
								lblError.setText("Passwords does not match");
								txtPassword.setText("");
								txtPassword2.setText("");
							}
						}
					});
				}
				{
					btnCancel = new JButton();
					this.add(btnCancel);
					btnCancel.setText("Cancel");
					btnCancel.setBounds(168, 282, 97, 20);
					btnCancel.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							GuiFacade.getInstance().showLogin();
						}
					});
				}
				{
					lblError = new JLabel();
					this.add(lblError);
					lblError.setBounds(77, 237, 10, 10);
				}
				{
					lblName = new JLabel();
					this.add(lblName);
					lblName.setText("Name:");
					lblName.setBounds(38, 37, 135, 14);
				}
				{
					lblPassword = new JLabel();
					this.add(lblPassword);
					lblPassword.setText("Password:");
					lblPassword.setBounds(38, 63, 135, 14);
				}
				{
					lblPass2 = new JLabel();
					this.add(lblPass2);
					lblPass2.setText("Confirm Password:");
					lblPass2.setBounds(38, 89, 135, 14);
				}
				{
					lblBday = new JLabel();
					this.add(lblBday);
					lblBday.setText("Birth Date:");
					lblBday.setBounds(38, 115, 135, 14);
				}
				{
					lblCountry = new JLabel();
					this.add(lblCountry);
					lblCountry.setText("Country:");
					lblCountry.setBounds(38, 141, 135, 14);
				}
				{
					lblCity = new JLabel();
					this.add(lblCity);
					lblCity.setText("City:");
					lblCity.setBounds(38, 167, 135, 14);
				}
				{
					txtName = new JTextField();
					this.add(txtName);
					txtName.setBounds(173, 36, 212, 20);
				}
				{
					txtPassword = new JPasswordField();
					this.add(txtPassword);
					txtPassword.setBounds(173, 62, 212, 20);
				}
				{
					txtPassword2 = new JPasswordField();
					this.add(txtPassword2);
					txtPassword2.setBounds(173, 88, 212, 20);
				}
				{
					txtBday = new JTextField();
					this.add(txtBday);
					txtBday.setBounds(173, 114, 212, 20);
				}
				{
					txtCountry = new JTextField();
					this.add(txtCountry);
					txtCountry.setBounds(173, 140, 212, 20);
				}
				{
					txtCity = new JTextField();
					this.add(txtCity);
					txtCity.setBounds(173, 166, 212, 20);
				}
				{
					lblHeadline = new JLabel();
					this.add(lblHeadline);
					lblHeadline.setText("Subscription");
					lblHeadline.setBounds(116, 10, 104, 14);
				}
				{
					lblPicture = new JLabel();
					this.add(lblPicture);
					lblPicture.setText("Picture:");
					lblPicture.setBounds(38, 193, 135, 14);
				}
				{
					txtPicture = new JTextField();
					this.add(txtPicture);
					txtPicture.setBounds(173, 190, 122, 20);
					txtPicture.setEditable(false);
				}
				{
					btnBrowse = new JButton();
					this.add(btnBrowse);
					btnBrowse.setText("Browse");
					btnBrowse.setBounds(301, 190, 84, 20);
					btnBrowse.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							JFileChooser fc = new JFileChooser(".");
							fc.setOpaque(true);
							fc.setMultiSelectionEnabled(false);
							fc.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "jpeg", "gif"));
							if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
								File f = fc.getSelectedFile();
								txtPicture.setText(f.getAbsolutePath());
							}
						}
					});
				}
			}
		} catch(Exception e) {
			SecurityHandler.catchException("initializing registration panel", e, false);
			//e.printStackTrace();
		}
	}
		
	/**
	 * check if 2 passwords are equal
	 * @param p1 first password
	 * @param p2 second password
	 * @return true if the passwords are equal
	 */
	private boolean checkPasswords(char[] p1, char[] p2){
		if (p1.length!=p2.length)
			return false;
		for (int i=0; i <p1.length ; i++){
			if (p1[i]!=p2[i])
				return false;
		}
		return true;
	}

}
